import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import '../base/base_state.dart';

class GoodsRefreshListView extends StatefulWidget {
  final List<Widget> children;
  final Widget? emptyWidget;
  final ScrollController? scrollController;

  const GoodsRefreshListView({
    Key? key,
    required this.children,
    this.emptyWidget,
    this.scrollController,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => GoodsRefreshListViewState();
}

class GoodsRefreshListViewState extends BaseState<GoodsRefreshListView> {
  EasyRefreshController? _controller;

  // 反向
  bool _reverse = false;

  // 方向
  Axis _direction = Axis.vertical;

  // Header浮动
  bool _headerFloat = false;

  // 无限加载
  bool _enableInfiniteLoad = true;

  // 控制结束
  bool _enableControlFinish = false;

  // 任务独立
  bool _taskIndependence = false;

  // 震动
  bool _vibration = true;

  // 是否开启刷新
  bool _enableRefresh = true;

  // 是否开启加载
  bool _enableLoad = true;

  // 顶部回弹
  bool _topBouncing = false;

  // 底部回弹
  bool _bottomBouncing = false;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
        enableControlFinishRefresh: true,
        enableControlFinishLoad: true,
        taskIndependence: _taskIndependence,
        controller: _controller,
        scrollController: widget.scrollController,
        reverse: _reverse,
        shrinkWrap: true,
        cacheExtent: 3,
        scrollDirection: _direction,
        topBouncing: _topBouncing,
        bottomBouncing: _bottomBouncing,
        header: ClassicalHeader(
            bgColor: Colors.blue,
            textColor: Colors.black,
            enableInfiniteRefresh: false,
            refreshReadyText: "加载上一级分类",
            refreshingText: "加载中...",
            refreshedText: "加载完成",
            showInfo: false,
            infoText: "",
            refreshText: "加载上一级分类"),
        footer: ClassicalFooter(
            bgColor: Colors.blue,
            textColor: Colors.black,
            enableInfiniteLoad: false,
            loadingText: "加载中...",
            infoText: "",
            showInfo: false,
            loadedText: "加载完成！",
            loadReadyText: "加载下一级分类",
            loadText: "加载下一级分类"),
        emptyWidget: widget.emptyWidget,
        slivers: [
          SliverList(
            delegate: SliverChildListDelegate(widget.children),
          )
        ],
        onLoad: () async {
          ///下一页
          await Future.delayed(const Duration(milliseconds: 500), () {
            if (mounted) {
              if (!_enableControlFinish) {
                _controller!.finishLoad(noMore: true);
                _controller!.resetLoadState();
              }
            }
          });
        },
        onRefresh: () async {
          await Future.delayed(const Duration(milliseconds: 500), () {
            if (mounted) {
              if (!_enableControlFinish) {
                _controller!.resetLoadState();
                _controller!.finishRefresh();
              }
            }
          });
        });
  }
}
